//
//  UseCAKeyFrameMediaTimeFunctionOnBallViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/31.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "UseCAKeyFrameMediaTimeFunctionOnBallViewController.h"

@interface UseCAKeyFrameMediaTimeFunctionOnBallViewController ()<CAAnimationDelegate>

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) UIImageView *ballImageV;

@end

@implementation UseCAKeyFrameMediaTimeFunctionOnBallViewController

/**  这一节我们模拟小球的弹起与掉落，想象一个橡胶球掉落到坚硬的地面的场景，当开始下落的时候，它会持续加速知道落到地面，然后经过几次反弹，最后停下来。
    这种效果没法用一个简单的三次贝塞尔曲线表示，于是不能用CAMediaTimingFunction来完成。但如果想要实现这样的效果，可以用如下几种方法：
 
 1、用CAKeyframeAnimation创建一个动画，然后分割成几个步骤，每个小步骤使用自己的计时函数。
 2、使用定时器逐帧更新实现动画
 
 这节运用第一种方法，第二种方法在第10张的demo里面可以找到
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backgroundView.backgroundColor = [UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1];
    [self.view addSubview:_backgroundView];
    
    self.ballImageV = [[UIImageView alloc] initWithFrame:CGRectMake(150, 50, 64, 64)]; // 此时center 182,82
    self.ballImageV.image = [UIImage imageWithName:@"ball"];
    [self.backgroundView addSubview:_ballImageV];
    
    UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    startButton.frame = CGRectMake(0, 450, 375, 30);
    startButton.backgroundColor = [UIColor whiteColor];
    [startButton setTitle:@"开始小球掉落动画" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
    startButton.tag = 100;
    [self.backgroundView addSubview:startButton];
    
  
}

- (void)start:(UIButton *)button
{
     [self startAnimation];
}

- (void)startAnimation
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    animation.duration = 2.0;
    animation.values = @[[NSValue valueWithCGPoint:CGPointMake(182, 82)],
                                              [NSValue valueWithCGPoint:CGPointMake(182, 400)],
                                              [NSValue valueWithCGPoint:CGPointMake(182, 190)],
                                              [NSValue valueWithCGPoint:CGPointMake(182, 400)],
                                              [NSValue valueWithCGPoint:CGPointMake(182, 300)],
                                              [NSValue valueWithCGPoint:CGPointMake(182, 400)],
                                              [NSValue valueWithCGPoint:CGPointMake(182, 350)],
                                              [NSValue valueWithCGPoint:CGPointMake(182, 300)]];
    animation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
                                                               [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
                                                               [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
                                                               [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
                                                               [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
                                                               [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
                                                               [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    animation.keyTimes = @[@0.0, @0.3, @0.5, @0.7, @0.8, @0.9, @0.95, @1.0];
    animation.delegate = self;
    [self.ballImageV.layer addAnimation:animation forKey:@"changePosition"];
}

- (void)animationDidStop:(CAKeyframeAnimation *)anim finished:(BOOL)flag
{
    self.ballImageV.layer.position = CGPointMake(182, 400);
}



@end
